package com.shangyueshang.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shangyueshang.common.SysConstants;
import com.shangyueshang.mapper.SysMenuMapper;
import com.shangyueshang.model.SysMenu;
import com.shangyueshang.service.SysMenuService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @author: JokeyZheng
 * @email: zhengjingfeng@ruqimobility.com
 * @created: 2022/1/5 18:49
 * @version: v1.0.0
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Override
    public List<SysMenu> findByUser(String username) {
        if (!StringUtils.hasText(username) || SysConstants.admin.equalsIgnoreCase(username)) {
            return super.list();
        }
        return baseMapper.findByUsername(username);
    }

    @Override
    public List<SysMenu> findByRole(Long roleId) {
        return baseMapper.findByRole(roleId);
    }

    @Override
    public List<SysMenu> findTree(String userName, int menuType) {
        List<SysMenu> treeMenus = new ArrayList<>();
        List<SysMenu> menus = this.findByUser(userName);
        for (SysMenu menu : menus) {
            if (menu.getParentId() == null || menu.getParentId() == 0) {
                menu.setLevel(0);
                if (!this.exists(treeMenus, menu)) {
                    treeMenus.add(menu);
                }
            }
        }
        treeMenus.sort(Comparator.comparing(SysMenu::getOrderNum));
        //查找子菜单
        this.findChildren(treeMenus, menus, menuType);

        return treeMenus;
    }

    /**
     * 查找子菜单
     *
     * @param treeMenus
     * @param menus
     * @param menuType
     */
    private void findChildren(List<SysMenu> treeMenus, List<SysMenu> menus, int menuType) {
        for (SysMenu treeMenu : treeMenus) {
            List<SysMenu> children = new ArrayList<>();
            for (SysMenu menu : menus) {
                // 如果是获取类型不需要按钮，且菜单类型是按钮的，直接过滤掉
                if (menuType == 1 && menu.getType() == 2) {
                    continue;
                }
                if (treeMenu.getId() != null && treeMenu.getId().equals(menu.getParentId())) {
                    menu.setParentName(treeMenu.getName());
                    menu.setLevel(treeMenu.getLevel() + 1);
                    if (!this.exists(children, menu)) {
                        children.add(menu);
                    }
                }
            }
            children.sort(Comparator.comparing(SysMenu::getOrderNum));
            treeMenu.setChildren(children);
            //递归处理
            this.findChildren(children, menus, menuType);
        }
    }

    private boolean exists(List<SysMenu> treeMenus, SysMenu sysMenu) {
        boolean exist = false;
        for (SysMenu menu : treeMenus) {
            if (menu.getId().equals(sysMenu.getId())) {
                exist = true;
            }
        }
        return exist;
    }
}
